﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FoodManager
{
    public partial class frNhapSanPhamChoDonHang : Form
    {
        public frNhapSanPhamChoDonHang()
        {
            InitializeComponent();
            //AutoCompleteStringCollection allSanPham = new AutoCompleteStringCollection();
            
            //cbSanPham.AutoCompleteCustomSource = allSanPham;
            //cbSanPham.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            dataGridView1.AutoGenerateColumns = false;
        }
        OrderClass CurrentOrder;
        public frNhapSanPhamChoDonHang(OrderClass currentOrder)
        {
            InitializeComponent();
            CurrentOrder = currentOrder;
        }

        private void frNhapSanPhamChoDonHang_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'db.tblsanpham' table. You can move, or remove it, as needed.
            this.tblsanphamTableAdapter.Fill(this.db.tblsanpham);
            BindGridviewData();
            
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            OrderDetail detail = new OrderDetail();
            detail.DonGia = Convert.ToDecimal(lblDonGia.Text);
            detail.IdOrder = 0;
            detail.Idsanpham = Convert.ToInt32(cbSanPham.SelectedValue);
            detail.SoLuong = Convert.ToDouble(nudKhoiLuong.Value);
            detail.TenSanPham = cbSanPham.Text;
            detail.ThanhTien = Convert.ToDecimal(detail.SoLuong) * detail.DonGia;
            detail.DonVi = lblDonVi.Text;
            this.CurrentOrder.AddDetail(detail);
            BindGridviewData();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
            foreach (DataGridViewRow row in rows)
            {
                OrderDetail o = row.DataBoundItem as OrderDetail;
                CurrentOrder.Remove(o);
            }
            BindGridviewData();
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            CurrentOrder.InsertOrder();
            CurrentOrder = null;
            MessageBox.Show("Đã nhập xong đơn hàng.");
            this.Close();
        }
        void BindGridviewData()
        {
            BindingSource soure = new BindingSource();
            soure.DataSource = CurrentOrder.ListDetail;
            dataGridView1.DataSource = soure;
            soure.ResetBindings(false);
        }
    }
}
